package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Quality;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.EquipmentMapper;
import com.ruoyi.system.domain.Equipment;
import com.ruoyi.system.service.IEquipmentService;

/**
 * 设备数据Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-20
 */
@Service
public class EquipmentServiceImpl implements IEquipmentService
{
    @Autowired
    private EquipmentMapper equipmentMapper;

    /**
     * 查询设备数据
     *
     * @param eId 设备数据主键
     * @return 设备数据
     */
    @Override
    public Equipment selectEquipmentByEId(Long eId)
    {
        return equipmentMapper.selectEquipmentByEId(eId);
    }

    /**
     * 查询设备数据列表
     *
     * @param equipment 设备数据
     * @return 设备数据
     */
    @Override
    public List<Equipment> selectEquipmentList(Equipment equipment)
    {
        return equipmentMapper.selectEquipmentList(equipment);
    }

    /**
     * 新增设备数据
     *
     * @param equipment 设备数据
     * @return 结果
     */
    @Override
    public int insertEquipment(Equipment equipment)
    {
        return equipmentMapper.insertEquipment(equipment);
    }

    /**
     * 修改设备数据
     *
     * @param equipment 设备数据
     * @return 结果
     */
    @Override
    public int updateEquipment(Equipment equipment)
    {
        return equipmentMapper.updateEquipment(equipment);
    }

    /**
     * 批量删除设备数据
     *
     * @param eIds 需要删除的设备数据主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentByEIds(Long[] eIds)
    {
        return equipmentMapper.deleteEquipmentByEIds(eIds);
    }

    /**
     * 删除设备数据信息
     *
     * @param eId 设备数据主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentByEId(Long eId)
    {
        return equipmentMapper.deleteEquipmentByEId(eId);
    }

    @Override
    public String importEquipment(List<Equipment> equipmentList, boolean updateSupport) {
        if (StringUtils.isNull(equipmentList) || equipmentList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Equipment equipment : equipmentList)
        {
            try
            {
                if (StringUtils.isNull(equipment.geteName()))
                {
                    throw new Exception("设备名称不能为空");
                }
                this.insertEquipment(equipment);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、设备名称 ").append(equipment.geteName()).append(" 导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、设备名称" + equipment.geteName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
